﻿using Inaction.UI.Extensions.API;
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.UI.Extensions
{
    [DisplayName("界面控件扩展管理")]
    [DesignerCategory("Inaction")]
    [ToolboxItem(true)]
    public partial class ExtensionHost : System.ComponentModel.Component, ISupportInitialize
    {
        #region 属性
        public override ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                if (base.Site != value)
                {
                    base.Site = value;
                    IReferenceService referenceService = (IReferenceService)this.GetService(typeof(IReferenceService));
                    if (referenceService != null)
                    {
                        object[] parent = referenceService.GetReferences(typeof(Form));
                        Form container = parent[0] as Form;
                        MainForm = container;
                    }
                }
            }
        }
        internal static Icon MainIcon
        {
            get;
            private set;
        }
        public Form MainForm { get; set; }
        #endregion

        #region 构造
        public ExtensionHost()
        {
            InitializeComponent();
        }
        public ExtensionHost(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }
        #endregion

        #region ISupportInitialize
        public void BeginInit()
        {
        }

        public void EndInit()
        {
            if (MainForm != null && !DesignMode)
            {
                var filter = new MessageFilter();
                filter.FilterMessageEvent += new MessageFilter.FilterMessageDelegate(FilterMessageEvent);
                Application.AddMessageFilter(filter);
                ExtensionHelper.RegeistExtenders();
                if (AppDomain.CurrentDomain.DomainManager == null)
                {
                    MainForm.Icon = System.Drawing.Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + AppDomain.CurrentDomain.SetupInformation.ApplicationName);
                }
                else
                {
                    MainForm.Icon = System.Drawing.Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.DomainManager.EntryAssembly.Location);
                }
                MainIcon = MainForm.Icon;
                MainForm.IsMdiContainer = true;
            }
        }

        void FilterMessageEvent(System.Windows.Forms.Message msg)
        {
            if (MainForm.IsDisposed) return;
            Form form;
            try
            {
                form = Application.OpenForms.Cast<Form>().FirstOrDefault(p => p.Handle == msg.HWnd);
                if (form != null)
                {
                    ExtensionHelper.ExtendControls(form);
                }
            }
            catch { return; }
        }
        #endregion
    }
}
